﻿using System;
using System.Windows;
using System.Windows.Media;
using System.ComponentModel;
using System.Collections.Generic;

using Microsoft.Maps.MapControl;
using Microsoft.Maps.MapControl.Core;

namespace SBPweb.Accelerators.StreetView.Client.BingMapsProvider.Models
{

    public class MapViewerModel : ModelBase, IMapViewer
    {

        #region Private fields

        #endregion

        #region Constructor logic

        internal MapViewerModel()
        {
        }

        #endregion

        #region Dependency properties

        public static readonly DependencyProperty ViewCenterProperty = DependencyProperty.Register("ViewCenter", typeof(Microsoft.Maps.MapControl.Location), typeof(MapViewerModel), null);
        public static readonly DependencyProperty ViewModeProperty = DependencyProperty.Register("ViewMode", typeof(MercatorMode), typeof(MapViewerModel), null);
        public static readonly DependencyProperty ZoomLevelProperty = DependencyProperty.Register("ZoomLevel", typeof(double), typeof(MapViewerModel), null);

        #endregion

        #region Property accessors

        public Microsoft.Maps.MapControl.Location ViewCenter
        {
            get
            {
                return GetValue<Microsoft.Maps.MapControl.Location>(ViewCenterProperty);
            }
            set
            {
                SetValue<Microsoft.Maps.MapControl.Location>(ViewCenterProperty, value, "ViewCenter");
            }
        }

        public MercatorMode ViewMode
        {
            get
            {
                return GetValue<MercatorMode>(ViewModeProperty);
            }
            set
            {
                SetValue<MercatorMode>(ViewModeProperty, value, "ViewMode");
            }
        }

        public double ZoomLevel
        {
            get
            {
                return GetValue<double>(ZoomLevelProperty);
            }
            set
            {
                SetValue<double>(ZoomLevelProperty, value, "ZoomLevel");
            }
        }

        #endregion

        #region Events

        public event EventHandler ViewChanging;

        public event EventHandler ViewChanged;


        //public event EventHandler<PolygonEventArgs> PolygonCreated;

        //public event EventHandler<PolygonEventArgs> RouteCreated;

        #endregion

        #region Methods

        #region View handling

        public void SetViewCenter(double latitude, double longitude)
        {
            ViewCenter = new Microsoft.Maps.MapControl.Location(latitude, longitude);
        }

        public void SetViewCenter(double latitude, double longitude, double altitude)
        {
            ViewCenter = new Microsoft.Maps.MapControl.Location(latitude, longitude, altitude);
        }

        public void SetZoomLevel(double zoomLevel)
        {
            ZoomLevel = zoomLevel;
        }


        public void RaiseViewChanging()
        {
            OnViewChanging();
        }

        public void RaiseViewChanged()
        {
            OnViewChanged();
        }

        #endregion

        #region Service handling

        public void FetchUserView()
        {
            //locationClient.GetUserViewAsync(this);
            //locationClient.GetUserPolygonsAsync(this);
            //locationClient.GetUserRoutesAsync(this);
        }

        #endregion

        #region Polygons

        //public void CreatePolygon(string name, Polygon polygon)
        //{
        //    OnPolygonCreated(name, polygon);
        //}

        //public void CreateRoute(string name, Polygon polygon)
        //{
        //    OnRouteCreated(name, polygon);
        //}

        #endregion

        #region Event handlers

        #region LocationClient

        //private void locationClient_GetUserViewCompleted(object sender, GetUserViewCompletedEventArgs e)
        //{
        //    if (e.UserState != this)
        //    {
        //        return;
        //    }
        //    if (e.Error != null)
        //    {
        //        throw e.Error;
        //    }

        //    ViewCenter = e.Result.Center;
        //    ZoomLevel = e.Result.ZoomLevel;
        //    switch (e.Result.Mode)
        //    {
        //        default:
        //            break;
        //        case ViewModes.Road:
        //            ViewMode = new RoadMode();
        //            break;
        //        case ViewModes.Aerial:
        //            ViewMode = new AerialMode(true);
        //            break;
        //        case ViewModes.AerialWithoutLabels:
        //            ViewMode = new AerialMode(false);
        //            break;
        //    }
        //}

        //private void locationClient_GetUserPolygonsCompleted(object sender, GetUserPolygonsCompletedEventArgs e)
        //{
        //    if (e.UserState != this)
        //    {
        //        return;
        //    }
        //    if (e.Error != null)
        //    {
        //        throw e.Error;
        //    }

        //    foreach (KeyValuePair<string, Polygon> kvp in e.Result)
        //    {
        //        OnPolygonCreated(kvp.Key, kvp.Value);
        //    }
        //}

        //private void locationClient_GetUserRoutesCompleted(object sender, GetUserRoutesCompletedEventArgs e)
        //{
        //    if (e.UserState != this)
        //    {
        //        return;
        //    }
        //    if (e.Error != null)
        //    {
        //        throw e.Error;
        //    }

        //    foreach (KeyValuePair<string, Polygon> kvp in e.Result)
        //    {
        //        OnRouteCreated(kvp.Key, kvp.Value);
        //    }
        //}

        #endregion

        #endregion

        #region Helper functions

        internal void OnViewChanging()
        {
            if (ViewChanging != null)
            {
                ViewChanging(this, EventArgs.Empty);
            }
        }

        internal void OnViewChanged()
        {
            if (ViewChanged != null)
            {
                ViewChanged(this, EventArgs.Empty);
            }
        }

        //internal void OnPolygonCreated(string name, Polygon polygon)
        //{
        //    if (PolygonCreated != null)
        //    {
        //        PolygonCreated(this, new PolygonEventArgs(name, polygon));
        //    }
        //}

        //internal void OnRouteCreated(string name, Polygon polygon)
        //{
        //    if (RouteCreated != null)
        //    {
        //        RouteCreated(this, new PolygonEventArgs(name, polygon));
        //    }
        //}

        #endregion

        #endregion

    }

}